import { Controller } from "../../mvc/core/Controller";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SRobCityThreeStart } from "../../../Message/Public/SRobCityThreeStart";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { SRobCityThreeJoinTeamInfo } from "../../../Message/Public/SRobCityThreeJoinTeamInfo";
import { SRobCityThreeJoinTeam } from "../../../Message/Public/SRobCityThreeJoinTeam";
import { IView } from "../../mvc/interfaces/IView";
import { RobCityModule } from "../../../modules/RobCityModule";
import { IRobCityModule } from "../../../modules/interfaces/IRobCityModule";
import { GuildRobCityCopyButton } from "../view/copy/guildRobCityCopy/GuildRobCityCopyButton";
import { ClockManager } from "../manager/ClockManager";
import { TimeEvent } from "../events/TimeEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GuildUtil } from "../view/guild/info/GuildUtil";
import { GameProxy } from "../mvc/GameProxy";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { TodayNoTipsConst } from "../../component/gconst/TodayNoTipsConst";
import { SCrossWorshipInfo } from "../../../Message/Public/SCrossWorshipInfo";
import { SLookupPlayerReply } from "../../../Message/Game/SLookupPlayerReply";
import { SGuildRobCityOneScoreList } from "../../../Message/Public/SGuildRobCityOneScoreList";
import { RobCityStepOnePanel } from "../view/robCity/stepOne/RobCityStepOnePanel";
import { RobCityStepOneRankItemData } from "../view/robCity/stepOne/RobCityStepOneRankItemData";
import { SGuildRobCityOneScore } from "../../../Message/Public/SGuildRobCityOneScore";
import { DataProvider } from "../../../fl/data/DataProvider";
import { SRobCityTwoJoinTeamInfo } from "../../../Message/Public/SRobCityTwoJoinTeamInfo";
import { SRobCityTwoStart } from "../../../Message/Public/SRobCityTwoStart";
type int = number;
//class RobCityController
    
    export  class RobCityController extends Controller
    {
       

        /* internal  */onRobCityThreeStartHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityThreeStart;
            cache.robCity.robCityCopyThreeStarted = true;
            cache.robCity.robCityCopyThreeStartInfo = loc1;
            this.showRobCityBtn();
            this.robCityBtn.copyType = ECopyType._ECopyRobCityThree;
            if (!cache.copy.isInCopyType(ECopyType._ECopyRobCityThree)) 
            {
                this.showToPanel(3);
            }
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityThreeStartInfo");
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionUpdateChatChanel));
            return;
        }

        /* internal  */onRobCityThreeEndHandler(arg1: MessageBlock): void
        {
            cache.robCity.robCityCopyThreeStarted = false;
            cache.robCity.robCityCopyThreeStartInfo = null;
            this.hideRobCityBtn();
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityThreeStartInfo");
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionUpdateChatChanel));
            return;
        }

        /* internal  */onReceiveRobCityThreeJoinTeamInfo(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=arg1.messageBase as SRobCityThreeJoinTeamInfo;
            if (loc1.robCityThreeJoinTeams) 
            {
                loc1.robCityThreeJoinTeams.sortOn("froce", Array.DESCENDING | Array.NUMERIC);
                loc3 = [];
                var loc5=0;
                var loc6=loc1.robCityThreeJoinTeams;
                for(loc4 of loc6) 
                {
                    if (loc4.shaBak > 0) 
                    {
                        loc2 = loc4;
                        continue;
                    }
                    loc3.push(loc4);
                }
                if (_view && !_view.isHide) 
                {
                    this.robCityModule.update("updateRobCityThreeTeamInfos", loc2, this.getTeam(loc3, 0), this.getTeam(loc3, 1), this.getTeam(loc3, 2), this.getTeam(loc3, 3));
                }
            }
            return;
        }

        /* internal  */getTeam(arg1: Array<any>, arg2: int): SRobCityThreeJoinTeam
        {
            if (arg1 == null || arg2 >= arg1.length) 
            {
                return null;
            }
            return arg1[arg2] as SRobCityThreeJoinTeam;
        }

        protected /* override */ initView(): IView
        {
            if (!this._robCityModule) 
            {
                this._robCityModule = new RobCityModule();
            }
            return this._robCityModule;
        }

        public get robCityModule(): IRobCityModule
        {
            return view as IRobCityModule;
        }

        public get robCityBtn(): GuildRobCityCopyButton
        {
            if (!this._robCityBtn) 
            {
                this._robCityBtn = new GuildRobCityCopyButton();
                this._robCityBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRobCityBtnClickHandler);
            }
            return this._robCityBtn;
        }

        /* internal  */showRobCityBtn(): void
        {
            this.robCityBtn.show();
            return;
        }

        /* internal  */hideRobCityBtn(): void
        {
            if (this._robCityBtn) 
            {
                this._robCityBtn.hide();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.RobCity_doOpenCopyOne, this.doOpenRobCityCopyOne);
            Dispatcher.addEventListener(EventName.RobCity_doEnterCopy, this.doEnterRobCityCopy);
            Dispatcher.addEventListener(EventName.RobCity_OpenRobCityModule, this.doOpenRobCityModule);
            Dispatcher.addEventListener(EventName.RobCity_CloseRobCityModule, this.doCloseRobCityModule);
            Dispatcher.addEventListener(EventName.CurWorldLevelUpdate, this.onCurWorldLevelUpdateHandler);
            ClockManager.instance.addEventListener(TimeEvent.DateChange, this.onDateChangeHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateWorshipDummyNum, this.onWorshipDummyNumHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSTChatelainInfoBack, this.onSTChatelainInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildRobCityOneScoreList, this.onReceiveRobCityOneScoreList);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityTwoJoinTeamInfo, this.onReceiveRobCityTwoJoinTeamInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityTwoPrepare, this.onRobCityTwoPrepareHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityTwoStart, this.onRobCityTwoStartHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityTwoEnd, this.onRobCityTwoEndHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityThreeJoinTeamInfo, this.onReceiveRobCityThreeJoinTeamInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityThreePrepare, this.onRobCityThreePrepareHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityThreeStart, this.onRobCityThreeStartHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityThreeEnd, this.onRobCityThreeEndHandler);
            return;
        }

        /* internal  */onRobCityBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._robCityBtn.copyType != ECopyType._ECopyRobCityOne) 
            {
                if (this._robCityBtn.copyType != ECopyType._ECopyRobCityTwo) 
                {
                    if (this._robCityBtn.copyType == ECopyType._ECopyRobCityThree) 
                    {
                        this.showToPanel(3);
                    }
                }
                else 
                {
                    this.showToPanel(2);
                }
            }
            else 
            {
                this.showToPanel(1);
            }
            return;
        }

        /* internal  */doOpenRobCityCopyOne(arg1: DataEvent): void
        {
            if (cache.role.curWorldLevel < this._lowWorldLevel) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60958));
                return;
            }
            if (GuildUtil.isLeader(cache.guild.myGuildInfo)) 
            {
                GameProxy.robCityProxy.openGuildRobCityOne();
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60409));
            }
            return;
        }

        /* internal  */doEnterRobCityCopy(arg1: DataEvent): void
        {
            var loc1=0;
            if (cache.role.curWorldLevel < this._lowWorldLevel) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60958));
                return;
            }
            if (cache.role.roleInfo.level >= this._lowLevel) 
            {
                loc1 = arg1.data as int;
                var loc2=loc1;
                switch (loc2) 
                {
                    case ECopyType._ECopyRobCityOne:
                    {
                        GameProxy.robCityProxy.enterGuildRobCityOne();
                        break;
                    }
                    case ECopyType._ECopyRobCityTwo:
                    {
                        GameProxy.robCityProxy.enterGuildRobCityTwo();
                        break;
                    }
                    case ECopyType._ECopyRobCityThree:
                    {
                        GameProxy.robCityProxy.enterGuildRobCityThree();
                        break;
                    }
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60921));
            }
            return;
        }

        /* internal  */doOpenRobCityModule(arg1: DataEvent): void
        {
            var loc1=arg1.data ? arg1.data as int : 0;
            this.showToPanel(loc1);
            return;
        }

        /* internal  */showToPanel(arg1: int): void
        {
            this.robCityModule.showToPanel(arg1);
            this.robCityModule.update("updateDate", cache.robCity.getRobCityStep());
            view.show();
            return;
        }

        /* internal  */doCloseRobCityModule(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                _view.hide();
            }
            if (this._robCityBtn) 
            {
                this._robCityBtn.hightLightBtn(false);
            }
            return;
        }

        /* internal  */onCurWorldLevelUpdateHandler(arg1: DataEvent): void
        {
            var loc1=false;
            if (cache.role.curWorldLevel == this._lowWorldLevel && cache.role.roleInfo.level >= 70) 
            {
                loc1 = PulseSharedObject.getValue(TodayNoTipsConst.RobCityOpenedTip);
                if (!loc1) 
                {
                    PulseSharedObject.saveValue(TodayNoTipsConst.RobCityOpenedTip, true);
                    this.showToPanel(0);
                }
            }
            return;
        }

        /* internal  */onDateChangeHandler(arg1=null): void
        {
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateDate", cache.robCity.getRobCityStep());
            }
            return;
        }

        /* internal  */onWorshipDummyNumHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossWorshipInfo;
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateWorshipDummyNum", loc1.dummyNum);
            }
            return;
        }

        /* internal  */onSTChatelainInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLookupPlayerReply;
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateSTChatelainInfo", loc1);
            }
            return;
        }

        /* internal  */onReceiveRobCityOneScoreList(arg1: MessageBlock): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            if (_view && !_view.isHide) 
            {
                if (!this._scoreListDp) 
                {
                    this.initList();
                }
                loc1 = arg1.messageBase as SGuildRobCityOneScoreList;
                loc3 = 0;
                while (loc3 < RobCityStepOnePanel.PageSize) 
                {
                    loc2 = this._scoreListDp.getItemAt(loc3) as RobCityStepOneRankItemData;
                    loc2.rank = (loc1.targetPage - 1) * loc1.perPage + loc3 + 1;
                    if (loc3 < loc1.guildRobCityOneScoreList.length) 
                    {
                        loc2.info = loc1.guildRobCityOneScoreList[loc3] as SGuildRobCityOneScore;
                    }
                    else 
                    {
                        loc2.info = null;
                    }
                    ++loc3;
                }
                this.robCityModule.update("updateRobCityOneScoreList", this._scoreListDp, loc1.targetPage, loc1.totalPage);
                this.robCityModule.update("updateRobCityOneMyGuildRank", loc1.myGuildRank);
            }
            return;
        }

        /* internal  */initList(): void
        {
            var loc1=null;
            this._scoreListDp = new DataProvider();
            var loc2=0;
            while (loc2 < RobCityStepOnePanel.PageSize) 
            {
                loc1 = new RobCityStepOneRankItemData();
                this._scoreListDp.addItem(loc1);
                ++loc2;
            }
            return;
        }

        /* internal  */onReceiveRobCityTwoJoinTeamInfo(arg1: MessageBlock): void
        {
            var loc3=null;
            var loc1=arg1.messageBase as SRobCityTwoJoinTeamInfo;
            var loc2=new Map<any, any> /* flash.utils.Dictionary */();
            var loc4=0;
            var loc5=loc1.robCityTwoJoinTeams;
            for(loc3 of loc5) 
            {
                if (!loc2[loc3.groupId]) 
                {
                    loc2[loc3.groupId] = [];
                }
                (loc2[loc3.groupId] as Array<any>).push(loc3);
            }
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityTwoGroupLists", this.getArr(loc2[1]), this.getArr(loc2[2]), this.getArr(loc2[3]), this.getArr(loc2[4]));
            }
            return;
        }

        /* internal  */getArr(arg1: Array<any>): Array<any>
        {
            if (arg1 == null) 
            {
                arg1 = [];
            }
            return arg1;
        }

        /* internal  */onRobCityTwoPrepareHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityTwoStart;
            cache.robCity.robCityCopyTwoStarted = false;
            cache.robCity.robCityCopyTwoStartInfo = loc1;
            this.showRobCityBtn();
            this.robCityBtn.copyType = ECopyType._ECopyRobCityTwo;
            this.robCityBtn.setLeftTime((loc1.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (!cache.copy.isInCopyType(ECopyType._ECopyRobCityTwo)) 
            {
                this.showToPanel(2);
            }
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityTwoStartInfo");
            }
            return;
        }

        /* internal  */onRobCityTwoStartHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityTwoStart;
            cache.robCity.robCityCopyTwoStarted = true;
            cache.robCity.robCityCopyTwoStartInfo = loc1;
            this.showRobCityBtn();
            this.robCityBtn.copyType = ECopyType._ECopyRobCityTwo;
            if (!cache.copy.isInCopyType(ECopyType._ECopyRobCityTwo)) 
            {
                this.showToPanel(2);
            }
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityTwoStartInfo");
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionUpdateChatChanel));
            return;
        }

        /* internal  */onRobCityTwoEndHandler(arg1: MessageBlock): void
        {
            cache.robCity.robCityCopyTwoStarted = false;
            cache.robCity.robCityCopyTwoStartInfo = null;
            this.hideRobCityBtn();
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityTwoStartInfo");
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionUpdateChatChanel));
            return;
        }

        /* internal  */onRobCityThreePrepareHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityThreeStart;
            cache.robCity.robCityCopyThreeStarted = false;
            cache.robCity.robCityCopyThreeStartInfo = loc1;
            this.showRobCityBtn();
            this.robCityBtn.copyType = ECopyType._ECopyRobCityThree;
            this.robCityBtn.setLeftTime((loc1.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (!cache.copy.isInCopyType(ECopyType._ECopyRobCityThree)) 
            {
                this.showToPanel(3);
            }
            if (_view && !_view.isHide) 
            {
                this.robCityModule.update("updateRobCityThreeStartInfo");
            }
            return;
        }

        private /* var */_lowLevel: int=70;

        private /* var */_lowWorldLevel: int=71;

        private /* var */_robCityModule: IRobCityModule;

        private /* var */_robCityBtn: GuildRobCityCopyButton;

        private /* var */_scoreListDp: DataProvider;
    }
